package com.medicine.hbp.dialog;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.picker.DatePicker;
import com.medicine.hbp.picker.TimePicker;

public class ChongfuTimeDialog {

	private Activity activity;

	private Dialog dialog;

	private ChongfuDialogTimeListener listener;

	private DatePicker dpicker;
	private TimePicker Tpicker;
	private Calendar calendar;
	private String selectDate, selectTime;
	// 选择时间与当前时间，用于判断用户选择的是否是以前的时间
	private int year, month, day, hour, monute;

	private String xingqi;

	private TextView text_time;
	private Button bnt_no;
	private Button bnt_yes;

	public ChongfuTimeDialog(Activity activity,
							 ChongfuDialogTimeListener listener) {
		this.activity = activity;
		this.listener = listener;
	}

	public void showDetailEditDialog() {
		View view = activity.getLayoutInflater().inflate(
				R.layout.chongfu_time_dialog, null);

		dialog = new Dialog(activity, R.style.transparentFrameWindowStyle);
		calendar = Calendar.getInstance();

		// 选择时间与当前时间的初始化，用于判断用户选择的是否是以前的时间，如果是，弹出toss提示不能选择过去的时间
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DAY_OF_MONTH);
		hour = calendar.get(Calendar.HOUR_OF_DAY );
		monute = calendar.get(Calendar.MINUTE);
		selectDate = year + "-" + month + "-" + day + " " + hour + ":" + monute;
		dpicker = (DatePicker) view.findViewById(R.id.date_picker);
		Tpicker = (TimePicker) view.findViewById(R.id.time_picker);
		text_time = (TextView) view.findViewById(R.id.text_time);
		bnt_no = (Button) view.findViewById(R.id.btn_no);
		bnt_yes = (Button) view.findViewById(R.id.btn_yes);
		dpicker.setOnChangeListener(dp_onchanghelistener);
		Tpicker.setOnChangeListener(tp_onchanghelistener);
		text_time.setText(selectDate);
		bnt_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();

			}
		});
		bnt_yes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				listener.onSelect(year+"年"+month+"月"+day+"日 "+xingqi+" "+hour+":"+monute,year+"/"+month+"/"+day+"  "+hour+":"+monute);
				dialog.dismiss();
			}
		});
		dialog.setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		Window window = dialog.getWindow();

		// 设置显示动画
		window.setWindowAnimations(R.style.show_photograpDialog_animstyle);
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		wl.width = width/5*4;
		wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		dialog.onWindowAttributesChanged(wl);
		dialog.show();
		dialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
						| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}

	public interface ChongfuDialogTimeListener {
		public void onSelect(String time,String stime);

		public void onCancel();
	}

	// listeners
	DatePicker.OnChangeListener dp_onchanghelistener = new DatePicker.OnChangeListener() {
		@Override
		public void onChange(int years, int months, int days, int day_of_week) {
			year = years;
			month = months;
			day = days;
			xingqi=DatePicker.getDayOfWeekCN(day_of_week);
			selectDate = year + "-" + month + "-" + day + " " + hour + ":"
					+ monute;
			text_time.setText(selectDate);
		}
	};
	TimePicker.OnChangeListener tp_onchanghelistener = new TimePicker.OnChangeListener() {
		@Override
		public void onChange(int hours, int minute) {
			hour = hours;
			monute = minute;
			selectDate = year + "-" + month + "-" + day + " " + hour + ":"
					+ monute;
			text_time.setText(selectDate);
		}
	};
}